package com.linkage.jz.bankruptcy.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.bean.MyProInfo;

import java.util.ArrayList;
import java.util.List;

import cn.bingoogolapple.androidcommon.adapter.BGARecyclerViewAdapter;
import cn.bingoogolapple.androidcommon.adapter.BGAViewHolderHelper;
import cn.bingoogolapple.swipeitemlayout.BGASwipeItemLayout;


public class RelationProListViewAdapter extends BGARecyclerViewAdapter<MyProInfo> {
    /**
     * 当前处于打开状态的item
     */
    private List<BGASwipeItemLayout> mOpenedSil = new ArrayList<>();
    private Context mcontext;

    private boolean needAction = true;

    public RelationProListViewAdapter(RecyclerView recyclerView, Context context) {
        super(recyclerView, R.layout.item_my_pro);
        mcontext = context;
//        super(recyclerView, R.layout.item_bgaswipe);
    }

    public void setNeedAction(boolean needAction) {
        this.needAction = needAction;
    }

    @Override
    public void setItemChildListener(BGAViewHolderHelper viewHolderHelper, int viewType) {
        BGASwipeItemLayout swipeItemLayout = viewHolderHelper.getView(R.id.sil_item_bgaswipe_root);
        swipeItemLayout.setDelegate(new BGASwipeItemLayout.BGASwipeItemLayoutDelegate() {
            @Override
            public void onBGASwipeItemLayoutOpened(BGASwipeItemLayout swipeItemLayout) {
                closeOpenedSwipeItemLayoutWithAnim();
                mOpenedSil.add(swipeItemLayout);
            }

            @Override
            public void onBGASwipeItemLayoutClosed(BGASwipeItemLayout swipeItemLayout) {
                mOpenedSil.remove(swipeItemLayout);
            }

            @Override
            public void onBGASwipeItemLayoutStartOpen(BGASwipeItemLayout swipeItemLayout) {
                closeOpenedSwipeItemLayoutWithAnim();
            }
        });
        viewHolderHelper.setItemChildClickListener(R.id.bt_item_bt1);
//        viewHolderHelper.setItemChildLongClickListener(R.id.tv_item_bgaswipe_delete);
        viewHolderHelper.setItemChildClickListener(R.id.bt_item_bt2);
//        viewHolderHelper.setItemChildLongClickListener(R.id.et_item_bgaswipe_read);
    }

    @Override
    public void fillData(BGAViewHolderHelper viewHolderHelper, int position, MyProInfo model) {
        String strStatus = "";
        viewHolderHelper.setVisibility(R.id.bt_item_bt1, View.GONE)
                .setVisibility(R.id.bt_item_bt2, View.GONE);
        viewHolderHelper.setTextColor(R.id.tv_item_status, R.color.page_top);
        if ("0".equalsIgnoreCase(model.getProjectStatus())) {
            strStatus = "暂存";
            viewHolderHelper.setTextColorRes(R.id.tv_item_status, R.color.black);
            viewHolderHelper.setBackgroundRes(R.id.bt_item_bt2,R.drawable.pro_g_corner_btn);
            viewHolderHelper.setBackgroundRes(R.id.bt_item_bt1,R.drawable.pro_h_corner_btn);
            viewHolderHelper.setText(R.id.bt_item_bt2, "继续编辑")
                    .setText(R.id.bt_item_bt1, "删除");

            viewHolderHelper.setVisibility(R.id.bt_item_bt1, View.VISIBLE)
                    .setVisibility(R.id.bt_item_bt2, View.VISIBLE);
        } else if ("1".equalsIgnoreCase(model.getProjectStatus())) {
            strStatus = "已关闭";
            viewHolderHelper.setTextColorRes(R.id.tv_item_status, R.color.darker_gray);
            viewHolderHelper.setVisibility(R.id.bt_item_bt1, View.GONE)
                    .setVisibility(R.id.bt_item_bt2, View.GONE);
        }  else if ("3".equalsIgnoreCase(model.getProjectStatus())) {
            strStatus = "已结案";
            viewHolderHelper.setTextColorRes(R.id.tv_item_status, R.color.darker_gray);
            viewHolderHelper.setVisibility(R.id.bt_item_bt1, View.GONE)
                    .setVisibility(R.id.bt_item_bt2, View.GONE);
        }else if ("2".equalsIgnoreCase(model.getProjectStatus())) {
            viewHolderHelper.setTextColorRes(R.id.tv_item_status, R.color.page_top);
            strStatus = "程序中";
//            if (!"".equalsIgnoreCase(model.getManagerId())) {
//                viewHolderHelper.setBackgroundRes(R.id.bt_item_bt1,R.drawable.pro_g_corner_btn);
//                viewHolderHelper.setText(R.id.bt_item_bt1, "管理项目");
//                viewHolderHelper.setVisibility(R.id.bt_item_bt1, View.VISIBLE)
//                        .setVisibility(R.id.bt_item_bt2, View.GONE);
//            }else{
                viewHolderHelper.setVisibility(R.id.bt_item_bt1, View.GONE)
                        .setVisibility(R.id.bt_item_bt2, View.GONE);
//            }
        }

        viewHolderHelper
                .setText(R.id.tv_item_status, strStatus)
                .setText(R.id.tv_item_bgaswipe_detail, model.getProjectName());

        if ("2".equalsIgnoreCase(model.getProjectStatus())&&!"".equalsIgnoreCase(model.getJoinId()) && "0".equalsIgnoreCase(model.getJoinType())
                && "0".equalsIgnoreCase(model.getAcceptInvitation())) {
            viewHolderHelper.setBackgroundRes(R.id.bt_item_bt2,R.drawable.pro_o_corner_btn);
            viewHolderHelper.setBackgroundRes(R.id.bt_item_bt1,R.drawable.pro_h_corner_btn);

        }
        String logo = model.getProjectLogo();

        String proName = model.getProjectName();


        if("".equalsIgnoreCase(logo))
        {
            if(proName.length()>2) {
                viewHolderHelper.setText(R.id.pro_title, proName.substring(0,2));
            }
        }else {
            viewHolderHelper.setText(R.id.pro_title, "");
            ImageView img = (ImageView)viewHolderHelper.getImageView(R.id.tv_item_bgaswipe_img);
            Glide.with(mcontext).load(model.getProjectLogo())
                    .placeholder(R.drawable.nopic)
                    .error(R.drawable.nopic).into(img);
        }



        BGASwipeItemLayout swipeItemLayout = viewHolderHelper.getView(R.id.sil_item_bgaswipe_root);
        swipeItemLayout.setSwipeAble(false);
    }



    public void closeOpenedSwipeItemLayoutWithAnim() {
        for (BGASwipeItemLayout sil : mOpenedSil) {
            sil.closeWithAnim();
        }
        mOpenedSil.clear();
    }
}